package com.example.mapstruct.controller;

import com.example.mapstruct.convert.StudentConvert;
import com.example.mapstruct.convert.ZooConvert;
import com.example.mapstruct.entity.student.StudentDao;
import com.example.mapstruct.entity.student.StudentModel;
import com.example.mapstruct.entity.student.StudentVO;
import com.example.mapstruct.entity.zoo.Cat;
import com.example.mapstruct.entity.zoo.Dog;
import com.example.mapstruct.entity.zoo.ZooDao;
import com.example.mapstruct.entity.zoo.ZooVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName MapStructController
 * @Description
 * @Author huyingliang
 * @Date 2024/1/15 17:31
 */
@RequestMapping("/mapstruct")
@RestController
public class MapStructController {

    /*获取 StudentConvert 实例*/
    StudentConvert studentConvert = StudentConvert.INSTANCE;

    /*获取 ZooConvert 实例*/
    ZooConvert zooConvert = ZooConvert.INSTANCE;


    @GetMapping("/test")
    public String test() {
        return "hello mapstruct";
    }

    @GetMapping("/test2")
    public String test2() {

        StudentDao dao = new StudentDao();
        dao.setAddress("北京");
        dao.setAge(12);
        dao.setName("张三");
        dao.setEmail("92877@qq.com");
        dao.setPhone("17684523659");

        StudentVO vo = studentConvert.dao2vo(dao);

        vo.setAge(13);
        System.out.println("vo = " + vo);
        System.out.println("dao = " + dao);

        dao.setAddress("上海");
        System.out.println("vo = " + vo);
        System.out.println("dao = " + dao);

        return "阿三大苏打";

        /*在这种 类中只是基础类型（ 基础类型的包装类 + String ）的情况下，不论改变 dao 还是 vo 都不会对
         * 另外一个对象产生影响*/
    }

    @GetMapping("/test3")
    public String test3() {

        /*这里测试 mapstruct 里面的 @Mapping 自定义的属性值转换*/
        StudentDao dao = new StudentDao();
        dao.setAddress("北京");
        dao.setAge(12);
        dao.setName("张三");
        dao.setEmail("92877@qq.com");
        dao.setPhone("17684523659");


        StudentModel model = studentConvert.dao2model(dao);

        return model.toString();
        //StudentModel(studentName=张三, studentAge=12, studentAddress=北京,
        // studentEmail=92877@qq.com, studentPhone=17684523659)

    }

    @GetMapping("/test4")
    public String test4() {

        /*   dao 转换成 vo 之后 ,
         *   对 dao 修改值，对 vo 无影响
         *   对 vo 修改值，对 dao 无影响
         * */

        ZooDao zooDao = new ZooDao();
        zooDao.setZooName("动物园");
        zooDao.setCat(new Cat("猫", 2));
        zooDao.setDog(new Dog("狗", 3));
        System.out.println("zooDao = " + zooDao);

        ZooVO zooVO = zooConvert.dao2Vo(zooDao);
        System.out.println("zooVO = " + zooVO);

        zooDao.setZooName("动物园2");
        System.out.println("zooDao = " + zooDao);
        System.out.println("zooVO = " + zooVO);

        zooVO.setCat(new Cat("猫2", 4));
        System.out.println("zooDao = " + zooDao);
        System.out.println("zooVO = " + zooVO);

        zooDao.setDog(new Dog("狗2", 5));
        System.out.println("zooDao = " + zooDao);
        System.out.println("zooVO = " + zooVO);

        return "test4";
    }


}
